﻿/* ==============================================================================
 * 功能描述：Monster  
 * 
 * 创 建 者：郑镇泉
 * 
 * 创建日期：2016/4/26 21:44:19
 * ==============================================================================*/
using UnityEngine;
using System.Collections;
using ZZQFrameWork.AI;
using ZZQFrameWork;

public class MonsterAI : EntityParent,IPool
{
    private MonsterVO data;

    public MonsterVO Data
    {
        get { return data; }
    }
    private BattleModule battleModule
    {
        get { return ModuleManager.Instance.Get<BattleModule>(); }
    }

    private uint battleTimeId;

    protected override void OnAwake()
    {
        root = BTManager.Instance.GetRoot(2);
    }

    public void Init(MonsterVO monster,Vector3 pos,Node node)
    {
        data = monster;
        transform.position = pos;
        nowNode = node;
        nowNode.canWalk = false;
        nowNode.isMonster = true;
        nowNode.goods = this.gameObject;
    }

    #region Proc

    protected override int CanThink()
    {
        int error = 0;
        if (nowNode == null) error = 1;
        else if (Data.hp <= 0) error = 2;
        return error;
    }

    public override Enum_BTState ProcAttackTo()
    {
        blackBoard.AIState = AIState.FIGHT_STATE;
        PlayerAI player = blackBoard.Enemy.GetComponent<PlayerAI>();
        player.BeHit(this, Data.atk);
        SoundManager.Instance.PlayEffect("5");
        blackBoard.AIState = AIState.FIGHT_CD_STATE;
        battleTimeId=TimerHeap.AddTimer((uint)Data.atkSpeed * 1000, 0, () =>
        {
            if (blackBoard.AIState == AIState.FIGHT_CD_STATE)
            blackBoard.AIState = AIState.FIGHT_STATE;
        });
        //if(blackBoard.AIState==AIState.IDLE_STATE)
        //return Enum_BTState.Success;
        return Enum_BTState.Running;
    }

    public override void BeHit(EntityParent attacker, int atk)
    {
        //可以在此设置仇恨值系统
        SelectNode = attacker.nowNode;
        blackBoard.Enemy = attacker.gameObject;

        int result = atk - Data.def;
        if (result <= 0) return;
        Data.DeductHp(result);
        if (Data.hp <= 0)
        {
            battleModule.EndBattle(this);
        }
    }   
    
    /// <summary>
    /// 停止战斗计时
    /// </summary>
    public void StopBattleTimer()
    {
        TimerHeap.DelTimer(battleTimeId);
    }

    #endregion


    public void Spawn()
    {
    }

    public void UnSpawn()
    {
    }
}